﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Main.Configs;
using Main.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Main.ViewModels
{
    public partial class MainViewModel:ObservableObject
    {
        [ObservableProperty]
        bool isLogin = false;

        [ObservableProperty]
        string? user;

        [RelayCommand]
        async Task LoginByToken()
        {
            // Configure open file dialog box
            var dialog = new Microsoft.Win32.OpenFileDialog();            
            dialog.DefaultExt = ".token"; // Default file extension
            dialog.Filter = "令牌文件|*.token"; // Filter files by extension

            // Show open file dialog box
            bool? result = dialog.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dialog.FileName;
                User = await ERPNextAuthentication.Authentication.GetUserByTokenFile(filename);
                if(!string.IsNullOrWhiteSpace(User))
                {
                    Serilog.Log.Information($"【系统】用户登录：{User}");
                    IsLogin = true;
                }
            }
        }

        [RelayCommand]
        void Logout()
        {
            if (MessageBox.Show("您确定要退出登录吗？",
                    "退出登录",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                Serilog.Log.Information($"【系统】用户退出：{User}");
                IsLogin = false;
                User = null;
            }

            
        }

        [RelayCommand]
        void GenerateJournalInputFile()
        {
            try
            {
                //
                string folder = Path.Combine(Directory.GetCurrentDirectory(), JournalEntryInputConfig.Current.JournalInputFolder);
                string defautFile = $"{DateTime.Now.ToString("yyyy-MM-dd")}.xlsx";

                //确保路径存在
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
                // Configure save file dialog box            
                var dialog = new Microsoft.Win32.SaveFileDialog();
                dialog.InitialDirectory = folder;
                dialog.FileName = defautFile;
                dialog.DefaultExt = ".xlsx"; // Default file extension
                dialog.Filter = "电子表格|*.xlsx"; // Filter files by extension

                // Show save file dialog box
                bool? result = dialog.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string destFile = dialog.FileName;
                    string srcFile = Path.Combine(Directory.GetCurrentDirectory(), "Templates", JournalEntryInputConfig.Current.JournalInputTemplateFile);

                    //
                    if (File.Exists(destFile))
                    {
                        File.Delete(destFile);
                    }
                    File.Copy(srcFile, destFile);

                    System.Diagnostics.Process.Start("Explorer.exe", destFile);
                }
            }
            catch(Exception ex) {

                Serilog.Log.Error($"【日记账凭证录入】错误：{ex.Message}");
            }
            
        }
    }
}
